const App = getApp();
const func = require('../../../utils/func');

Page({
    /**
     * 页面的初始数据
     */
    data: {
        // 售后参数
        page: 1,
        total: 0,
        perPage: 0,
        lastPage: 1,
        isLoading: true,
        loadService: false,
        nullService: false,
        shopService: {},

        // 售后订单状态
        dataType: 0,
        // 列表容器高度
        scrollHeight: null,
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        let _this = this;
        // 设置scroll-view高度
        _this.setListHeight();
    },

    /**
     * 设置商品列表高度
     */
    setListHeight() {
        let systemInfo = wx.getSystemInfoSync(),
            rpx = systemInfo.windowWidth / 750, // 计算rpx
            tapHeight = Math.floor(rpx * 88), // tap高度
            scrollHeight = systemInfo.windowHeight - tapHeight; // swiper高度
        this.setData({scrollHeight});
    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        let _this = this;
        // 查询服务订单列表
        _this.getServiceOrderList();
    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {
        let _this = this;
        _this.data.page = 1;
        _this.data.lastPage = 1;
        // 获取订单列表
        _this.getServiceOrderList();
        wx.stopPullDownRefresh();
    },

    // 上拉加载
    onReachBottom() {
        let _this = this;
        if (_this.data.perPage >= _this.data.total) return false;
        _this.setData({loadService: true});
        if (_this.data.page >= _this.data.lastPage) {
            _this.setData({nullService: true, loadService: false, isLoading: false});
            return false;
        }
        _this.getServiceOrderList(true, ++_this.data.page);
    },

    // 查询服务订单列表
    getServiceOrderList(isPage, page) {
        let _this = this;
        let postData = {
            page: page || 1,
            action: 'getServiceList',
        };
        App._requestApi(_this, App.globalData.config.shopOrderServiceActionUrl, postData, function (res) {
            // 显示页面
            _this.setData({nullService: false, loadService: false, isLoading: false});
            // 订单数据
            let resService = res.data.data;
            if (isPage == true) {
                if (resService.length == 0) {
                    _this.setData({nullService: true});
                } else {
                    let shopService = _this.data.shopService;
                    _this.setData({shopService: shopService.concat(resService)});
                }
            } else {
                _this.setData({
                    total: res.data.total,
                    shopService: resService,
                    perPage: res.data.per_page,
                    lastPage: res.data.last_page,
                });
            }
        });
    },

    // 跳转订单详情页
    navigateToDetail(e) {
        let _this = this;
        let service_id = e.currentTarget.dataset.id;
        wx.navigateTo({
            url: '../service/detail?service_id=' + service_id
        });
    },

});